package fem2d;

import java.util.ArrayList;

import fem2.AbstractStructuralStaticDemo;
import fem2.Constraint;
import fem2.Element;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshPart;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.analysis.Analysis;
import fem2.analysis.NonlinearStaticAnalysis;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.material.FluencyCriteria;
import fem2.material.LinearIsotropicHardeningRule;
import fem2.material.LinearKinematicHardeningRule;
import fem2.material.VonMisesMM;
import fem2.pre_and_post.GidMeshGenerator;

public class Beam2dVonMises extends AbstractStructuralStaticDemo {

	String fn;

	public Beam2dVonMises() {
		projectDir = "/home/hbui/kratos_janosch";
		projectName = "beam2d.gid";
		fn = projectDir + '/' + projectName + '/' + "mesh1.msh";
	}

	@Override
	public Mesh createMesh() {
		MeshGenerator mg = new GidMeshGenerator(fn);
		return mg.getMesh(2);

		// MeshGenerator mg = new PatchMeshGenerator(241, 21, 0, 0, 0.05, 0.05);
		// return mg.getMesh(2);
	}

	@Override
	public Model createConditions(Model m) {
		Constraint c1 = new Constraint(false, false);
		Mesh mesh = m.getMesh();
		ArrayList<Node> left = MeshUtilities.seekNodesOnSurface(mesh, 1, 0, 0);
		mesh.addConstraint(c1, left);

		Constraint c2 = new Constraint(true, false);
		c2.setValue(1, -0.1);
		MeshUtilities.seekNode(mesh, 12, 0).setConstraint(c2);

		return m;
	}

	@Override
	public MaterialModel createMaterial(Model m) {
		double E = 25850;
		double nu = 0.18;
		double t = 200;
		double ft = 2.7;
		double H = E / 2;
		double K = E / 2;
		FluencyCriteria isoLaw = new LinearIsotropicHardeningRule(ft, K);
		FluencyCriteria kinLaw = new LinearKinematicHardeningRule(H);
		// return new VonMisesMMNoVar(E, nu, t, 0, State.PLANE_STRAIN, isoLaw,
		// kinLaw);
		return new VonMisesMM(E, nu, t, 0, State.PLANE_STRAIN, isoLaw, kinLaw);
	}

	@Override
	public Element createElement(MeshPart mp, MaterialModel mm) {
		return new StructuralElement(mp, mm);
	}

	@Override
	public void run() {
		Model m = createModel();
		/*
		 * analysis
		 */
		Analysis an = new NonlinearStaticAnalysis(m, 10);
		an.getStrategy().setEchoLevel(EchoLevelType.OUTPUT, EchoLevelType.AUX1);
		an.run();

	}

	public static void main(String[] args) {
		Beam2dVonMises demo = new Beam2dVonMises();

		demo.run();

		// Model m = demo.createModel();
		// m.getMesh().Print();
	}
}
